var star = "*";

//Function formValidation() checks if all form are filled when submitting the form values
function formValidation() {
	if (nameValidation() == true && emailValidation() == true && surnameValidation() == true) {
		alert("You are now a registered member!!!");
		document.register_form.submit();
		return true;
	} else {
		nameValidation();
		emailValidation();
		surnameValidation();
		password1Validation();
		password2Validation();
		alert("Not all fields are filled properly!!!");
		return false;
	}
}

//Function nameValidation() is used to check if the name is written correctly
//Checks if only the characters are written in the field
function nameValidation() {
    var fld_name = document.register_form.register_name;
    var letter_check = /[\W\d]/i;
	if (fld_name.value == "" || letter_check.test(fld_name.value)) {
		document.getElementById("imgName").src = "registration_validation/images/error.png";
		document.getElementById("errName").innerHTML = "Enter your name correctly!"; 
		document.getElementById("starName").innerHTML = star.sup();
		fld_name.style.backgroundColor = "#fde910"; 
		return false;
    } else {
		document.getElementById("imgName").src = "registration_validation/images/ok.png";
		document.getElementById("errName").innerHTML = ""; 
		document.getElementById("starName").innerHTML = "";
		fld_name.style.backgroundColor = ""; 
		return true;
	}
	
}

//Function nameValidation() is used to check if the surname is written correctly
//Checks if only the characters are written in the field
function surnameValidation() {
    var fld_name = document.register_form.register_surname;
    var letter_check = /[\W\d]/i;
	if (fld_name.value == "" || letter_check.test(fld_name.value)) {
		document.getElementById("imgSurName").src = "registration_validation/images/error.png";
		document.getElementById("errSurName").innerHTML = "Enter your name correctly!"; 
		document.getElementById("starSurName").innerHTML = star.sup();
		fld_name.style.backgroundColor = "#fde910"; 
		return false;
    } else {
		document.getElementById("imgSurName").src = "registration_validation/images/ok.png";
		document.getElementById("errSurName").innerHTML = ""; 
		document.getElementById("starSurName").innerHTML = "";
		fld_name.style.backgroundColor = ""; 
		return true;
	}
	
}

//Function emailValidation() is used to check if the email is written correctly
//Checks "@", "." and the common structure of email address
function emailValidation() {
	var fld_email = document.register_form.register_email;
	var email_check = /^\w+([a-z0-9\-\_\.])([a-z0-9]+)\@([a-z0-9]+)([a-z0-9\-\.]*)([a-z0-9]+)\.([a-z]{2,6})$/i;
	if (fld_email.value == "" || !email_check.test(fld_email.value)) {
		document.getElementById("imgEmail").src = "registration_validation/images/stop.png";
		document.getElementById("errEmail").innerHTML = "Enter your email correctly!"; 
		document.getElementById("starEmail").innerHTML = star.sup();
		fld_email.style.backgroundColor = "#fde910"; 
		return false;
    } else {
		document.getElementById("imgEmail").src = "registration_validation/images/ok.png";
		document.getElementById("errEmail").innerHTML = ""; 
		document.getElementById("starEmail").innerHTML = "";
		fld_email.style.backgroundColor = "";
		return true;	
	}
	
}

//Checks the validness of the password1 field.
function password1Validation() {
	var password1 = document.register_form.register_pass1;
	if (password1.value == "") {
		document.getElementById("imgPass1").src = "registration_validation/images/error.png";
		document.getElementById("errPass1").innerHTML = "Password should not be blank!"; 
		document.getElementById("starPass1").innerHTML = star.sup();
		password1.style.backgroundColor = "#fde910"; 
		return false;
	} else if (password1.value.length <= 3 ) { 
		document.getElementById("imgPass1").src = "registration_validation/images/error.png";
		document.getElementById("errPass1").innerHTML = "Password is too short!";  
		document.getElementById("starPass1").innerHTML = star.sup();
		password1.style.backgroundColor = "#fde910"; 
		return false;
	} else {
		document.getElementById("imgPass1").src = "registration_validation/images/ok.png";
		document.getElementById("errPass1").innerHTML = ""; 
		document.getElementById("starPass1").innerHTML = "";
		password1.style.backgroundColor = "";
		return true;
	}
}

//Checks the validness of the password2 field.
function password2Validation() {
	var password2 = document.register_form.register_pass2;
	if (password2.value == "") {
		document.getElementById("imgPass2").src = "registration_validation/images/stop.png";
		document.getElementById("errPass2").innerHTML = "Password should not be blank!"; 
		document.getElementById("starPass2").innerHTML = star.sup();
		password2.style.backgroundColor = "#fde910"; 
		return false;
	} else if (password2.value != document.register_form.register_pass1.value) { 
		document.getElementById("imgPass2").src = "registration_validation/images/stop.png";
		document.getElementById("errPass2").innerHTML = "Passwords don't match!"; 
		document.getElementById("starPass2").innerHTML = star.sup();
		password2.style.backgroundColor = "#fde910"; 
		return false;
	} else {
		document.getElementById("errPass2").style.color = "green";
		document.getElementById("errPass2").innerHTML = "Passwords match";
		document.getElementById("imgPass2").src = "registration_validation/images/ok.png";
		document.getElementById("starPass2").innerHTML = ""; 
		password2.style.backgroundColor = ""; 
		return true;
	}
}
